package org.softee.functional.functions;

public class BinaryOperators {

    private BinaryOperators() {

    }

    public static <T extends Comparable<? super T>> BinaryOperator<T> max() {
        return new BinaryOperator<T>() {
            @Override
            public T apply(T op1, T op2) {
                // TODO null handling
                return op1.compareTo(op2) < 0 ? op2 : op1;
            }
        };
    }

    public static <T extends Comparable<? super T>> BinaryOperator<T> min() {
        return new BinaryOperator<T>() {
            @Override
            public T apply(T op1, T op2) {
                // TODO null handling
                return op1.compareTo(op2) > 0 ? op2 : op1;
            }
        };
    }

    public static BinaryOperator<Integer> integerAdd() {
        return new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer op1, Integer op2) {
                return op1 + op2;
            }
        };
    }

    public static BinaryOperator<Integer> integerMultiply() {
        return new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer op1, Integer op2) {
                return op1 * op2;
            }
        };
    }

    public static BinaryOperator<Long> longAdd() {
        return new BinaryOperator<Long>() {
            @Override
            public Long apply(Long op1, Long op2) {
                return op1 + op2;
            }
        };
    }

    public static BinaryOperator<Long> longMultiply() {
        return new BinaryOperator<Long>() {
            @Override
            public Long apply(Long op1, Long op2) {
                return op1 * op2;
            }
        };
    }

    public static BinaryOperator<String> concatenate() {
        return new BinaryOperator<String>() {
            @Override
            public String apply(String op1, String op2) {
                return op1 + op2;
            }
        };
    }

    public static BinaryOperator<String> join(final String separator) {
        return new BinaryOperator<String>() {
            @Override
            public String apply(String op1, String op2) {
                return (op1 == null) ? op2 : op1 + separator + op2;
            }
        };
    }
}
